% 2021.10.14
% HW2

close all;
clc;
clear;
% open_system('HW2_SLX')
result_sim = sim('homework2_SLX');
x1 = result_sim.x1.Data;
x2 = result_sim.x2.Data;
x3 = result_sim.x3.Data;
x_all = [x1 x2 x3];
xg = result_sim.xg.Data;

% 20 averages，见Welch方法，https://www.jianshu.com/p/bbd3975e594f
N = 60;
% 1000，N点离散傅里叶变换NDFT(nfft)，与cpsd分辨率有关，过低会导致呈折xian
num_DFT = 500;
% []默认50%overlap
% 见MATLAB文档cpsd-Input Arguments-noverlap-
% "If you specify noverlap as empty..."
figure('name', 'hanning')
cpsd(x_all, xg, hanning(N), [], num_DFT, 1000);
legend('cpsd_{\bf\itx_1}_{\bf\itx_g}', 'cpsd_{\bf\itx_2}_{\bf\itx_g}', 'cpsd_{\bf\itx_3}_{\bf\itx_g}')

figure('name', 'hanning')
cpsd(x_all, xg, hanning(N), [], num_DFT, 1000)
legend('cpsd_{\bf\itx_1}_{\bf\itx_g}', 'cpsd_{\bf\itx_2}_{\bf\itx_g}', 'cpsd_{\bf\itx_3}_{\bf\itx_g}')

% 对实验室数据与实际xg互反应谱
exact_x1 = result_sim.exact_x1.Data;
exact_x2 = result_sim.exact_x2.Data;
exact_x3 = result_sim.exact_x3.Data;
exact_x_all = [exact_x1 exact_x2 exact_x3];
exact_xg = result_sim.exact_xg.Data;

figure('name', 'hanning_exact')
cpsd(exact_x_all, exact_xg, hanning(N), [], num_DFT, 1000)
legend('cpsd_{exact{\bf\itx_1}}_{exact{\bf\itx_g}}','cpsd_{exact{\bf\itx_2}}_{exact{\bf\itx_g}}', 'cpsd_{exact{\bf\itx_3}}_{exact{\bf\itx_g}}')
